<?php
namespace zzcms\primary\controller;

use think\Controller;
use think\facade\Env;

/**
 * 工具操作类
 * Class Tools
 * @package zzcms\admin\controller
 */
class Tools extends Controller
{

    /**
     * 主站后台传值验证码更新缓存
     */
    public function clearCache()
    {
        //检测密码
        $pass = $this->request->has('pass') ? $this->request->param('pass') : '';
        if ($pass != 'pos188168198') {
            return array2json(['status' => 0, 'msg' => '更新失败']);
        }
        $runtime_path = Env::get('runtime_path');
        $this->deleteFile($runtime_path . '/cache');
        //deleteFile(LOG_PATH);
        $this->deleteFile($runtime_path . '/temp');
        return array2json(['status' => 1, 'msg' => '更新缓存成功']);
    }

    /**
     * 删除目录下所有文件
     * @param $dir_path 目录
     */
    private function deleteFile($dir_path)
    {
        $fp = dir($dir_path);
        while (false != ($item = $fp->read())) {
            if ($item == '.' || $item == '..') {
                continue;
            }
            if (is_dir($fp->path . '/' . $item)) {
                $this->deleteFile($fp->path . '/' . $item);
                rmdir($fp->path . '/' . $item);
            } else {
                unlink($fp->path . '/' . $item);
            }
        }
    }

}